home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / util / vmake.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-28  |  11.5 KB  |  516 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. /*****************************************************************************
  26. *
  27. * vmake.c
  28. *
  29. *    HDF Vset utility.
  30. *
  31. *    vmake:  utility creates vsets. May be used in 3 ways:
  32. *            (1)    add a new vgroup.
  33. *            (2)    add a new vdata from ascii data.
  34. *            (3)    link vgroups and vdatas to a vgroup.
  35. *
  36. *    USAGE:
  37. *            (1)     vmake file vgname
  38. *            (2)    vmake file vsname {format} {ascii data stream}
  39. *            (3)    vmake file -l vgref v1 v2 ... vn
  40. *
  41. ******************************************************************************/
  42.  
  43. #include "vg.h"
  44.  
  45. /*
  46.  * Prototypes of local functions
  47.  */
  48. int32 vsetlink
  49.   PROTO((char *hfile, int32 vgid, int32 ids[], int32 n));
  50.  
  51. int32 vgadd
  52.   PROTO((char *hfile, char *vgname));
  53.  
  54. int32 inpdata 
  55.   PROTO((unsigned char**bp));
  56.  
  57. void vsadd
  58.   PROTO((char *hfile, char *vsname, char *format));
  59.  
  60. int32 scanit
  61.   PROTO((char *string, char ***fields, int32 **type, int32 **order));
  62.  
  63. int32 savfld
  64.   PROTO((char *ss, int p1, int p2));
  65.  
  66. int32 compact
  67.   PROTO((char *ss, char *dd));
  68.  
  69. int32 savtype
  70.   PROTO((char *ss, int p1, int p2));
  71.  
  72. int32 separate
  73.   PROTO((char *ss, char *fmt, int32 *num));
  74.  
  75. /*
  76.  *  Main entry point
  77.  */
  78. main(ac,av) int ac; char**av; {
  79.  
  80.   char  *hfile, *vgname, *vsname, *fmt;
  81.  
  82.   if (ac <3 ) {
  83.         show_help_msg();
  84.         exit(0);
  85.         }
  86.   else if (ac==3)  {
  87.       if (!strcmp(av[2],"-l")) { show_help_msg() ; exit(0); }
  88.           hfile  = av[1];
  89.           vgname = av[2];
  90.          vgadd(hfile,vgname);
  91.         }
  92.  
  93.   else if (ac==4)  {
  94.       if (!strcmp(av[2],"-l")) { show_help_msg() ; exit(0); }
  95.           hfile    = av[1];
  96.           vsname   = av[2];
  97.           fmt      = av[3];
  98.          vsadd(hfile,vsname,fmt);
  99.         }
  100.  
  101.   else if (!strcmp(av[2],"-l")) {
  102.  
  103.          int i;
  104.     int32 n, vgref, ids[50];
  105.           hfile     = av[1];
  106.         sscanf(av[3],"%d",&vgref);
  107.         for(n=0,i=4;i<ac;i++,n++) { sscanf(av[i],"%d",&ids[n]); }
  108.         vsetlink(hfile,vgref,ids,n);
  109.         }
  110.  
  111.   else {
  112.         show_help_msg();
  113.         exit(0);
  114.         }
  115.  
  116.   } /* main */
  117.   
  118. void showfmttypes() {
  119.     fprintf(stderr,"\tvalid fmt types: \n");
  120.     fprintf(stderr,"\t  c - char (text) \n");
  121.     fprintf(stderr,"\t  b - byte \n"); 
  122.     fprintf(stderr,"\t  d - integer \n");
  123.     fprintf(stderr,"\t  l - long (32)\n");
  124.     fprintf(stderr,"\t  f - float \n");
  125.     }
  126.  
  127. int show_help_msg()  {
  128.  
  129.   printf("\nvmake: creates vsets.\n");
  130.   printf("\nUSAGE:\n");
  131.   
  132.   printf(" (1) vmake file vgname             (adds a new vgroup)\n");
  133.   printf(" (2) vmake file vsname format      (adds a new vdata)\n");
  134.   printf(" (3) vmake file -l vgref v1 .. vn  (links v1 v2 .. vn into vgref)\n");
  135.   
  136.   printf("\nwhere\n");
  137.   printf("  vgref is the ref of a vgroup\n");
  138.   printf("  v1,..,vn are refs of vgroups and vdatas\n");
  139.   printf("  format is <field=fmt,field=fmt,..>\n");
  140.   printf("    field is any text string\n");
  141.   printf("    fmt is one of the following optionally preceded by a decimal.\n");
  142.   showfmttypes();
  143.   
  144.   printf("\nTo create a vdata, vmake reads ascii data from stdin\n");
  145.   
  146.   printf("EXAMPLES:\n");
  147.   printf("\t cat dat.txt | vmake hh.hdf \"triangles\" \"PLIST3=3d\"\n");
  148.   printf("\t vmake abc.hdf \"xyvals\" \"X=d,Y=f\" < abc.dat\n");
  149.   printf("\n");
  150.   
  151.   return (1);
  152.   
  153. } /* show_help_msg */
  154.  
  155. /* ------------------------------------------------------- */
  156.  
  157. #ifdef PROTOTYPE
  158. int32 vsetlink(char *hfile, int32 vgid, int32 ids[], int32 n)
  159. #else
  160. int32 vsetlink(hfile,vgid,ids,n)
  161. char * hfile;
  162. int32 vgid, n, ids[];
  163. #endif
  164. {
  165.   HFILEID f;
  166.   VGROUP * vgmain, *vg;
  167.   VDATA * vs;
  168.   int32 err=0;
  169.   int32 i;
  170.  
  171.     f = DFvsetopen(hfile,DFACC_ALL,0);
  172.     if (f == FAIL) {
  173.       fprintf(stderr,"cannot open %s.  \n",hfile); exit(0); 
  174.       }
  175.  
  176.   vgmain = (VGROUP*) Vattach(f,vgid,"w");
  177.   if(vgmain==NULL) { fprintf(stderr, "0\n"); DFvsetclose(f); exit(-1);}
  178.  
  179.   for(i=0;i<n;i++) {
  180.       if     ( -1 != vexistvg(f,ids[i])) {
  181.             if ((vg=(VGROUP*) Vattach(f,ids[i],"r")) != NULL)  {
  182.              if (Vinsert(vgmain,(VDATA*) vg)  == -1)  { /*  is really VGROUP* */
  183.                     err = 1; 
  184.                    fprintf(stderr,"insert a vg (%d)fails!!\n",ids[i]);    
  185.                     }
  186.               Vdetach(vg);
  187.               }
  188.          }
  189.      else if ( -1 != vexistvs(f,ids[i])) {
  190.            if ((vs= (VDATA*) VSattach(f,ids[i],"r")) != NULL) { 
  191.              if (Vinsert(vgmain,(VDATA*) vs) == -1) { 
  192.                     err = 1;
  193.                    fprintf(stderr,"insert a vs (%d)fails!!\n",ids[i]);    
  194.                     }
  195.               VSdetach(vs);
  196.               }
  197.          }
  198.      else { fprintf(stderr,"no such vgroup or vdata [%d]\n",ids[i]);  err= 1; }
  199.       }
  200.  
  201.   Vdetach(vgmain);
  202.   DFvsetclose(f);
  203.  
  204.   if (err) exit(-1); 
  205.   else fprintf(stderr,"1\n"); /* success */
  206.   return (1);
  207. } /* vsetlink */
  208.  
  209. /* ------------------------------------------------------- */
  210. /* 
  211. add a (new) vgroup to the file 
  212. */
  213.  
  214. #ifdef PROTOTYPE
  215. int32 vgadd (char *hfile, char *vgname) 
  216. #else
  217. int32 vgadd (hfile,vgname) 
  218. char * hfile;
  219. char * vgname;
  220. #endif
  221. {
  222.   HFILEID f;
  223.   int32 ref; 
  224.   VGROUP * vg;
  225.  
  226.   f=DFvsetopen(hfile,DFACC_ALL,0);
  227.   if (f==FAIL) {
  228.       fprintf(stderr,"cannot open %s. \n",hfile); exit(0); 
  229.       }
  230.  
  231.   vg = (VGROUP*) Vattach(f,-1,"w");
  232.   if (vg==NULL) { fprintf(stderr,"cannot attach vg\n"); exit(0); }
  233.   ref = vg->oref;
  234.   Vsetname(vg,vgname);
  235.   Vdetach(vg);
  236.  
  237.   DFvsetclose(f);
  238.   fprintf(stderr,"%d\n",ref);
  239.   return(1);
  240.  
  241. } /* vgadd */
  242.  
  243. /* ------------------------------------------------------- */
  244. /*
  245.  add a (new) vdata to the file.
  246.  Data will be ascii and will come in from stdin
  247.  according to the format (c-style).
  248. */
  249. #ifdef PROTOTYPE
  250. void vsadd (char *hfile, char *vsname, char *format)
  251. #else
  252. void vsadd (hfile,vsname,format)
  253. char * hfile;
  254. char * vsname;
  255. char * format;
  256. #endif
  257. {
  258.   int32 stat, i,n, nwritten;
  259.   unsigned char *buf;
  260.   char **fields;
  261.   int32 *type, *order, nfld;
  262.   char allfields[100];
  263.   HFILEID    f;
  264.   VDATA *vs;
  265.   int32 ref,ftype;
  266.  
  267.   nfld = scanit(format,&fields,&type,&order);
  268.   if (nfld < 1) { fprintf(stderr,"bad fields\n"); exit(-1); }
  269.  
  270.   if ( (f=DFvsetopen(hfile,DFACC_ALL,0))==FAIL) {
  271.       fprintf(stderr,"cannot open %s.  \n",hfile); exit(-1); 
  272.       }
  273.  
  274.   vs = (VDATA*) VSattach(f,-1,"w");
  275.   ref = vs->oref;
  276.  
  277. printf("vsadd: ref is %d\n",ref);
  278.  
  279.   allfields[0] = '\0';
  280.   for (i=0;i<nfld;i++) {
  281.       switch (type[i]) {
  282.           case 'c': ftype = LOCAL_CHARTYPE;  break;
  283.           case 'd': ftype = LOCAL_INTTYPE;   break;
  284.           case 'f': ftype = LOCAL_FLOATTYPE; break;
  285.           case 'l': ftype = LOCAL_LONGTYPE;  break;
  286.           
  287.           case 'b': ftype = LOCAL_BYTETYPE;  break; 
  288.           case 's': ftype = LOCAL_SHORTTYPE;  break; 
  289.           case 'D': ftype = LOCAL_DOUBLETYPE;  break; 
  290.  
  291.           default:  fprintf(stderr,"bad type [%c]\n",type[i]); 
  292.                         showfmttypes();
  293.                         exit(-1);
  294.                         break;
  295.           }
  296.       stat = VSfdefine(vs,fields[i],ftype,order[i]);
  297.       strcat(allfields,fields[i]);
  298.       strcat(allfields,",");
  299.      }
  300.  
  301.   i=HDstrlen(allfields); allfields[i-1]='\0'; /* remove last comma */
  302.  
  303.   VSsetname(vs,vsname);
  304.   stat = VSsetfields(vs,allfields);
  305.  
  306.   nwritten = 0;
  307.   while( (n = inpdata(&buf)) > 0) {
  308.      /*  printf("inpdata rets n=%d .. ",n); */
  309.     stat = VSwrite(vs,buf,n,FULL_INTERLACE);
  310.      printf("+%d",stat); 
  311.      nwritten +=n;
  312.      if (stat < 1) fprintf(stderr,"Vswrite stat=%d\n",stat);
  313.      }
  314.   VSdetach(vs);
  315.  
  316.   DFvsetclose(f);
  317.   fprintf(stderr,"%d\n",ref,nwritten);
  318.   return;
  319.  
  320. } /* vsadd */
  321.  
  322. /* ------------------------------------------------------------------ */
  323. /* This part of the code deals with formatting stdin input data.      */
  324. /* ------------------------------------------------------------------ */
  325.  
  326. #include <stdio.h>
  327.  
  328.  
  329. #define MAXVAR 32
  330. static char *fldptr[MAXVAR];
  331. static char flds[MAXVAR][100];
  332. static char fmts[MAXVAR]; 
  333. static int32  fords[MAXVAR]; 
  334. static int32  ftyp[MAXVAR]; 
  335. static int  ntotal = 0;
  336.  
  337.  
  338. /* scanf functions */
  339. static int32 inpint  (x) int  *x; { return(scanf ("%d ",x)); }
  340. static int32 inpfloat(x) float*x; { return(scanf ("%f ",x)); }
  341. static int32 inpchar (x) char *x; { return(scanf ("%c ",x)); }
  342. static int32 inplong (x) long *x; { return(scanf ("%ld ",x)); }
  343.  
  344.  
  345.  
  346. #define BUFSIZE 40000
  347.  
  348. int32 inpdata (bp) 
  349.      unsigned char**bp; 
  350.   int32 totalsize, nread, t,i,j,k;
  351.   unsigned char *b;
  352.   int32 maxrec;
  353.   int32 (*inpfn[MAXVAR])();
  354.   int32 inpsiz[MAXVAR];
  355.   unsigned char inpbuffer[BUFSIZE];
  356.  
  357.      for(i=0;i<ntotal;i++) {
  358.         switch(fmts[i]) {
  359.  
  360.            case 'c':
  361.               inpfn[i]  = inpchar; inpsiz[i] = sizeof(char);
  362.               break;
  363.  
  364.            case 'b':
  365.               inpfn[i]  = inpchar; inpsiz[i] = sizeof(char);
  366.               break;
  367.  
  368.            case 'd':
  369.              inpfn[i]  = inpint; inpsiz[i] = sizeof(int);
  370.               break;
  371.  
  372.            case 'l':
  373.               inpfn[i]  = inplong; inpsiz[i] = sizeof(long);
  374.               break;
  375.  
  376.  
  377.            case 'f':
  378.               inpfn[i] = inpfloat; inpsiz[i] = sizeof(float);
  379.               break;
  380.  
  381.  
  382.             default: printf("inpdata: fmt routine for [%c] not ready\n",fmts[i]);
  383.            } 
  384.         }
  385.      for (totalsize=0, i=0;i<ntotal;i++) totalsize  += (fords[i]*inpsiz[i]); 
  386.       maxrec = BUFSIZE/totalsize - 1; 
  387.  
  388.   /* begin reading in the ascii data from stdin */
  389.  
  390.     *bp = b = inpbuffer;
  391.    for (nread=0, j=0;j<maxrec;j++,nread++) {
  392.       for(i=0;i<ntotal;i++) {
  393.            for(k=0;k<fords[i];k++) {
  394.               t = (inpfn[i])(b);  
  395.             if (t==EOF)  return(nread); 
  396.               b+=inpsiz[i];
  397.               }
  398.            }
  399.       }
  400.  
  401.    return (nread); /* no of recs read */
  402.  
  403. } /* inpdata */
  404.  
  405.  
  406. int32 scanit (string,fields,type,order)
  407.  char *   string;
  408.  char *** fields;
  409.  int32  **  type;
  410.  int32  **  order;
  411.  {
  412.   int32 ns,i;
  413.   int32 p1,p2;
  414.   char ss[300];
  415.   int32 c;
  416.  
  417.  
  418.  compact(string,ss);
  419.  ns = HDstrlen(ss); ss[ns++] = ',';
  420.  
  421.  p1 = p2 = 0;
  422.  for(i=0;i<ns;i++) {
  423.      c = ss[i];
  424.      if(c== '=') {
  425.          p2 = i;
  426.          savfld(ss,p1,p2-1);
  427.          p1 = p2+1;
  428.          }
  429.      else if(c== ',') {
  430.          p2 = i;
  431.          savtype(ss,p1,p2-1);
  432.          p1 = p2+1;
  433.          }
  434.      }
  435.    for(i=0;i<ntotal;i++) {
  436.       fldptr[i] = flds[i];
  437.       ftyp[i]   = fmts[i];
  438.       }
  439.  
  440.    *type   =  ftyp;
  441.    *order  =  fords;
  442.     *fields =  fldptr;
  443.    return (ntotal);
  444.  
  445. } /* scanit */
  446.  
  447. #ifdef PROTOTYPE
  448. int32 compact (char *ss, char *dd) 
  449. #else
  450. int32 compact (ss,dd) 
  451.      char *ss, *dd; 
  452. #endif
  453. {
  454.   int i,t,n = HDstrlen(ss);
  455.   for(t=0,i=0;i<n;i++) if(ss[i]!=' ') { dd[t++] = ss[i]; }
  456.   dd[t] = '\0';
  457.   return (1);
  458. }
  459.  
  460. /* ------------------------------------------------------------------ */
  461. #ifdef PROTOTYPE
  462. int32 savfld(char *ss, int p1, int p2)
  463. #else
  464. int32 savfld(ss,p1,p2) 
  465.      char *ss; 
  466.      int p1,p2; 
  467. #endif
  468. {
  469.   int32 t=p2-p1+1;
  470.   strncpy(flds[ntotal],&ss[p1],t);
  471.   flds[ntotal][t] = '\0';
  472.   return (1);
  473.  
  474. } /* savfld */
  475.  
  476. #ifdef PROTOTYPE
  477. int32 savtype (char *ss, int p1, int p2) 
  478. #else
  479. int32 savtype (ss,p1,p2) 
  480.      char *ss; 
  481.      int p1,p2; 
  482. #endif
  483. {
  484.   char temp[20];
  485.   int32 t=p2-p1+1;
  486.   strncpy(temp,&ss[p1],p2-p1+1); temp[t] = '\0';
  487.   separate(temp,&fmts[ntotal],&fords[ntotal]);
  488.   ntotal++;
  489.   return (1);
  490.  
  491.   }
  492.  
  493. #ifdef PROTOTYPE
  494. int32 separate(char *ss, char *fmt, int32 *num) 
  495. #else
  496. int32 separate(ss,fmt,num) 
  497. char *ss; 
  498. char *fmt; 
  499. int32 *num; 
  500. #endif
  501. {
  502.   int32 i,n;
  503.   i=0;
  504.   n=HDstrlen(ss);
  505.   while(i<n) {
  506.     if(ss[i]<'0' ||  ss[i] >'9') break; 
  507.     i++;
  508.   }
  509.   if(i>0) sscanf(ss,"%d",num); else *num= 1;
  510.   *fmt  = ss[i];
  511.   return (1);
  512. }
  513.  
  514. /* ------------------------------------------------------------------ */
  515.